// SERVER SIDE

// This is run when server first starts, or on map change or the script is changed
// This really is not NEEDED but, you never know when weird glitches may happen
for r,s in pairs(player.GetAll()) do
if s:IsValid() then
s:SetFrags(0)
s:SetDeaths(0)
s:SetPData("KSPoints", 0)
s:SetPData("KSPointsTime", 0)
end
end
GameHasBeenWon = 0



// Created goal limit script if not made already.
local DefaultGoalLimit  = "25"
if (not file.IsDir("TDM/GoalLimit","DATA")) then
    file.CreateDir("TDM/GoalLimit")
end
if not(file.Exists("TDM/GoalLimit/GoalLimit.txt","DATA")) then
	file.Write("TDM/GoalLimit/GoalLimit.txt", DefaultGoalLimit)
	print("[TDM] Wrote file : TDM/GoalLimit/GoalLimit.txt")
end


// Changes the the goal from 25 to a specified user amount (This one is for Derma and Shortcut)
function TDMCustomGoalLimiterChk(ply, cmd, args) -- Basically had to add it twice due to a glitch with [1] and shortcuts
local args = tonumber(args)
if type(args) != "number" then 
umsg.Start( "TDM_Goal_Not_Number", ply )
umsg.End() 
return end
if args <= 0 then 
umsg.Start( "TDM_Goal_Is_Zero", ply )
umsg.End()
return end
if args > math.floor(args) then 
umsg.Start( "TDM_Goal_Has_Decimal", ply )
umsg.End()
return end
if ply:IsSuperAdmin() then
	if (not file.IsDir("TDM/GoalLimit","DATA")) then
		file.CreateDir("TDM/GoalLimit")
	end
	if not(file.Exists("TDM/GoalLimit/GoalLimit.txt","DATA")) then
		file.Write("TDM/GoalLimit/GoalLimit.txt", args)
		print("[TDM] Wrote file : TDM/GoalLimit/GoalLimit.txt")
	end
		file.Write("TDM/GoalLimit/GoalLimit.txt", args)
	umsg.Start( "TDM_Changed_Score_Limit" )
		umsg.String(ply:Nick())
		umsg.Short(ply:Team())
		umsg.String(args)
	umsg.End() 
	file.Append( "TDM/Logs/"..os.date("%m-%d-%y")..".txt", os.date("%I:%M:%S %p").." | "..ply:Nick().." has set the global goal to: "..args.." \n" )
/*  Allows to set score every time menu is open.
local GetPlayers = player.GetAll()
	for k,v in pairs(GetPlayers) do
	if v:IsValid() then
		local GetScore4File = file.Read("TDM/GoalLimit/GoalLimit.txt")
		umsg.Start("PlaceScoreForMenu",v)
		umsg.Short(k)
		umsg.Short(GetScore4File)
		umsg.End()
	end
end
*/
else
umsg.Start( "TDM_Not_Admin", ply )
umsg.End() 
end
end

/*
function CheckPlayerPointsXP2()
	timer.Create( "CheckXPOnPlayers2", 5, 0, function()
		MsgN("tewst")
	end)
end
timer.Destroy("CheckXPOnPlayers2")
timer.Simple( 1, function()
CheckPlayerPointsXP2()
end)
*/

// Changes to see if Goal equals nil, zero, or a string which is not a number (This one is for the Derma Menu)
function TDMCustomGoalLimiter( ply, cmd, args )
if ply:IsSuperAdmin() then
local args = tonumber(args[1])
if type(args) != "number" then 
umsg.Start( "TDM_Goal_Not_Number", ply )
umsg.End() 
return end
if args <= 0 then 
umsg.Start( "TDM_Goal_Is_Zero", ply )
umsg.End()
return end
if args > math.floor(args) then 
umsg.Start( "TDM_Goal_Has_Decimal", ply )
umsg.End()
return end
TDMCustomGoalLimiterChk(ply, cmd, args)
else
umsg.Start( "TDM_Not_Admin", ply )
umsg.End()
end
end
concommand.Add( "TDMCustomGoalLimiter", TDMCustomGoalLimiter )



// Team Goal Checker and Frags Set
function PlayerDeathGoalTDM( ply, wep, killer )
if killer == ply then
	local KilledSelfFragSet = killer:Frags() + 1
	killer:SetFrags(KilledSelfFragSet)
	local KilledSelfFragDeaths = ply:Deaths() - 1
	ply:SetDeaths(KilledSelfFragDeaths)
	
elseif killer:Team() == ply:Team() then
	local LowerTKFrags = killer:Frags() - 1
	killer:SetFrags(LowerTKFrags)
	
	if ply:Deaths() >= 0 then
		local RaiseTKDeaths = ply:Deaths() - 1
		ply:SetDeaths(RaiseTKDeaths)
	else
	return end
end

if killer == nil || killer == NULL || !killer:IsValid() then return end
if ply == nil || ply == NULL || !ply:IsValid() then return end
-- if ply:IsBot() || killer:IsBot() then return end -- Kept for testing purposes only
if killer:Team() == ply:Team() then return end
-- Will add score by one from killer
local ScoreFindTeam = killer:Team()
local GetCurrentScore = team.GetScore(ScoreFindTeam)
if GetCurrentScore == nil then
GetCurrentScore = 0
end
local AddScoreOne = (GetCurrentScore + 1)
team.SetScore( ScoreFindTeam, AddScoreOne )
--MsgN("Team: "..team.GetName(ScoreFindTeam).." score is now: "..AddScoreOne)
file.Append( "TDM/Logs/"..os.date("%m-%d-%y")..".txt", os.date("%I:%M:%S").." | "..ply:Nick().." has killed "..killer:Nick().." \n" )

-- Checks Score
for k,v in pairs(team.GetAllTeams()) do
	if k != 0 and k < 1000 then
		if team.GetScore(k) == tonumber(file.Read("TDM/GoalLimit/GoalLimit.txt","DATA")) then
		pl = player.GetAll()[1]
			for l,r in pairs(player.GetAll()) do
				if r:Frags() > pl:Frags() then
				pl = r
				end
			end
			GameHasBeenWon = 1
			umsg.Start( "TDM_Game_Won" )
			umsg.String(pl:Nick())
			umsg.Short(k)
			umsg.String(pl:Frags())
			umsg.End() 
			file.Append( "TDM/Logs/"..os.date("%m-%d-%y")..".txt", os.date("%I:%M:%S").." | "..team.GetName(k).." has won the game. MVP was "..pl:Nick().." with "..pl:Frags().." kills! \n" )
			timer.Create("TDM-Restart-Map", 20, 1, function()
			game.ConsoleCommand("changelevel "..game.GetMap( ).."\n")
			end)
		end
	end
end
end
hook.Add( "PlayerDeath", "PlayerDeathGoalTDM", PlayerDeathGoalTDM ) 



// Checks to see if friendly fire is on or off, then checks to see if game finished
function PlayerShouldTakeDamageTDM( ply, attacker)
if attacker:IsPlayer() && attacker:IsValid() then
ply:SetPData("GetLastWeapon", ply:GetActiveWeapon():GetClass())
local CheckForFF = tonumber(file.Read("TDM/Friendlyfire/friendlyfire.txt","DATA"))
if( attacker:Team() == ply:Team() and CheckForFF == 0 ) then
	if GameHasBeenWon == 1 then
		return false
	else
		return false
	end
	end
end
	if GameHasBeenWon == 1 then
		return false
	else
		return true
	end
end
hook.Add( "PlayerShouldTakeDamage", "PlayerShouldTakeDamageTDM", PlayerShouldTakeDamageTDM)
 

 
// Created friendly fire script if not made already.
local DefaultFriendlyFire  = "0"
if (not file.IsDir("TDM/Friendlyfire","DATA")) then
    file.CreateDir("TDM/Friendlyfire")
end
if not(file.Exists("TDM/Friendlyfire/friendlyfire.txt","DATA")) then
	file.Write("TDM/Friendlyfire/friendlyfire.txt", DefaultFriendlyFire)
	print("[TDM] Wrote file : TDM/Friendlyfire/friendlyfire.txt")
end



// Puts friendly fire on or off
function TDMFriendlyFireButton( ply, cmd, args )
if ply:IsSuperAdmin() then
if (not file.IsDir("TDM/Friendlyfire","DATA")) then
    file.CreateDir("TDM/Friendlyfire")
end
if not(file.Exists("TDM/Friendlyfire/friendlyfire.txt","DATA")) then
	file.Write("TDM/Friendlyfire/friendlyfire.txt", tonumber(args[1]))
	print("[TDM] Wrote file : TDM/Friendlyfire/friendlyfire.txt")
end
	file.Write("TDM/Friendlyfire/friendlyfire.txt", tonumber(args[1]))
if tonumber(args[1]) == 1 then
FriendlyFireChk = "on"
else
FriendlyFireChk = "off"
end
umsg.Start( "TDM_Changed_Friendly_Fire" )
umsg.String(ply:Nick())
umsg.Short(ply:Team())
umsg.String(FriendlyFireChk)
umsg.End() 
file.Append( "TDM/Logs/"..os.date("%m-%d-%y")..".txt", os.date("%I:%M:%S").." | "..ply:Nick().." has turned friendly fire "..FriendlyFireChk.." \n" )
else
umsg.Start( "TDM_Not_Admin", ply )
umsg.End()
end
end
concommand.Add( "TDMFriendlyFireButton", TDMFriendlyFireButton )



// Shortcut Commands to config tab in menu
function TDMShortcutCommands(ply,said)
	local words = string.Explode(" ", said)
	if words[1] == "!ff" then
		if words[2] == "on" then
		local ffsaid = "1"
		TDMFriendlyFireButton( ply, cmd, ffsaid )
		end
		if words[2] == "off" then
		local ffsaid = "0"
		TDMFriendlyFireButton( ply, cmd, ffsaid )
		end
	end
	
	if words[1] == "!goal" then
	if words[2] == "0" || words[2] == 0 || words[2] < "0" then 
	umsg.Start( "TDM_Goal_Is_Zero", ply )
	umsg.End()
	return end
	TDMCustomGoalLimiterChk( ply, cmd, words[2] )
	end
	
	if words[1] == "!armour" then
		if words[2] == "on" then
		local armorsaid = "1"
		TDMArmorButton( ply, cmd, armorsaid )
		end
		if words[2] == "off" then
		local armorsaid = "0"
		TDMArmorButton( ply, cmd, armorsaid )
		end
	end
end
hook.Add("PlayerSay","TDMShortcutCommands",TDMShortcutCommands)